<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pagesize:10
				},
				goodsList:[],
				total:0,
				// 设置节流阀
				isLoading:false
			};
		},
		onLoad(options){
			// console.log(options)
			this.queryObj.cid = options.cid
			this.queryObj.query = options.query
			this.getGoodsList()
		},
		methods:{
			async getGoodsList(cb){
				// 打开节流阀
				this.isLoading = true
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search?=' + this.queryObj)
				// 关闭节流阀
				this.isLoading = false
				cb && cb()
				// console.log(res)
				if(res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList,...res.message.goods]
				this.total = res.message.total
				
			},
			gotoDetail(goods){
				// console.log(goods)
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_detail=' + goods.goods_id
				})
			}
		},
		onReachBottom(){
			// 判断下一页是否还有数据
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			// 判断节流阀状态
			if(this.isLoading) return
			// 页码自增
			this.queryObj.pagenum ++
			// console.log(this.queryObj.pagenum)
			this.getGoodsList()
		},
		onPullDownRefresh() {
			// 1.重置数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			
			// 2.重发请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		
	}
</script>

<style lang="scss">

</style>
